//---------------------------------------------------------------------
//  This file is part of the Background Motion solution.
// 
//  Copyright (C) Mindscape (TM).  All rights reserved.
//  http://www.mindscape.co.nz
// 
//  THIS CODE AND INFORMATION ARE PROVIDED AS IS WITHOUT WARRANTY OF ANY
//  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
//  PARTICULAR PURPOSE.
//---------------------------------------------------------------------

using System.Collections.Generic;

namespace Mindscape.BackgroundMotion.Core
{
  /// <summary>
  /// Provides a mechanism to locate dependant components to allow for Inversion of Control
  /// </summary>
  public sealed class ServiceLocator
  {
    private static IServiceBroker _serviceBroker;

    /// <summary>
    /// Locates and returns a service from the active broker
    /// </summary>
    /// <typeparam name="T">The generic type of the service contract being requested</typeparam>
    /// <returns>The object which the fulfills that contract as located by the active broker</returns>
    public static T Get<T>()
    {
      T item = default(T);

      try
      {
        item = _serviceBroker.Get<T>();
      }
      catch (KeyNotFoundException)
      {
      }

      if (item == null)
      {
      }

      return item;
    }

    /// <summary>
    /// Initializes the locator based on the specified service broker.
    /// </summary>
    /// <param name="serviceBroker">The service broker which will locate services.</param>
    public static void Initialize(IServiceBroker serviceBroker)
    {
      _serviceBroker = serviceBroker;
    }
  }
}